package ge.modules.drawables;

import ge.engine.Log;
import ge.engine.Positionable;
import ge.modules.Module;

import java.util.ArrayList;

public class MultiLineTextModule implements Module,Positionable
{

	private ArrayList<TextModule> textos;
	private String font;
	private double X,Y;
	private double despl;
	private Color color;
	private int layer;
	private boolean visible;
	/**
	 *  Multi line text module permits you to draw text in different lines.
	 *  It's just composed by a list of text modules for each line to be drawn. 
	 * @param font The font of all the texts
	 * @param space Space between the lines
	 */
	public MultiLineTextModule( String font, double space)
	{
		this.font = font;
		textos = new ArrayList<TextModule>();
		X = Y = 0;
		this.despl = space;
		layer = 0;
		color = new Color();
		visible = true;
	}
	/**
	 * 	All the '\n' of the string will separated it in different lines.
	 *  Clear all text
	 * @param text Text to be drawn
	 */
	public void setText(String text)
	{
		textos.clear();
		addText(text);
	}
	
	/**
	 * 
	 * @param layer Layer to draw the text
	 */
	public void setLayer(int layer)
	{
		this.layer = layer;
		for(TextModule tx : textos)
			tx.setLayer(layer);
	}
	
	public int getLayer()
	{
		return layer;
	}
	/**
	 * 	All the '\n' of the string will separated it in different lines.
	 *  Is added at the last text.
	 * @param text Text to be drawn
	 */
	public void addText(String text)
	{
		String[] lines = text.split("\n");
		
		for(String s : lines)
		{
			
			TextModule t = new TextModule(s, font);
			t.setLayer(layer);
			t.setPos(X, Y+despl*textos.size());
			t.setColor(color);
			textos.add(t);
		 
		}		
	}
	@Override
	public void update() 
	{
		
		 for(TextModule t : textos)
		 { 
			 t.update();
		 }
	}

	@Override
	public void draw()
	{
		if(!visible)
			return;
		
		for(TextModule t : textos)
			 t.draw();

	}
	/**
	 * 	Makes the multitext visible or not
	 * @param visible
	 */
	public void setVisible(boolean visible)
	{
		this.visible = visible;
	}
	/**
	 * 
	 * @return If it's visible or not
	 */
	public boolean getVisible()
	{
		return visible;
	}
	@Override
	public void setPos(double x, double y) 
	{
		X = x;
		Y = y;
		
		for(int i = 0; i < textos.size(); i++)
		{
			textos.get(i).setPos(X, Y+despl*i);
		}
	}
	@Override
	public double getX() {
		  
		return X;
	}
	@Override
	public double getY() {
		 
		return Y;
	}
	/**
	 * 
	 * @return Return the color of the text
	 */
	public Color getColor()
	{
		return color;
	}
	/**
	 *  Changes the color of all the text
	 * @param c new Color
	 */
	public void setColor(Color c)
	{
		for(TextModule t : textos)
			t.setColor(c);
		
		color = c;
	}
	@Override
	public void delete() {
		for(TextModule t : textos)
			t.delete();
		
	}
}
